class Group < ActiveRecord::Base
  belongs_to :share
  belongs_to :account
  has_many :operations, :conditions => 'subgroup_id is null'
  has_many :subgroups, :conditions => 'quantity > 0'

  def self.init
    res = self.new
    res.quantity = 0
    res.outcome = 0
    res.brokerage = 0
    res.income = 0
    res.balance = 0
    res
  end


  def self.listOpen
    find(:all,
         :conditions => "quantity > 0")
  end

  def self.listClosed
    find(:all,
         :conditions => "quantity = 0")
  end

  def self.byOpenShare(share_id)
    find(:first,
         :conditions => [ "quantity > 0 and share_id = ?", share_id])
  end

  def allOperations
    result = operations
    subgroups.each do |subgroup|
      result = result.concat(subgroup.operations)
    end
    result
  end

  def averagePurchasePrice
    count = 0
    amount = 0
    operations.select {|operation|
      operation.operation_type_id == 1
    }.each {|operation|
      count += operation.quantity
      amount += operation.net_amount
    }  
    unless count == 0
      amount/count
    else
      0
    end
  end

  def averageSalePrice
    count = 0
    amount = 0
    operations.select {|operation|
      operation.operation_type_id == 2
    }.each {|operation|
      count += operation.quantity
      amount += operation.net_amount
    }
    unless count == 0
      amount/count
    else
      0
    end
  end


  def purchase(operation)
    operation.operation_type_id = 1
    operation.group_id = id
    operation.share_id = share_id
    operation.net_amount = operation.quantity * operation.price
    operation.gross_amount = operation.net_amount + operation.brokerage

    Group.transaction do
      self.quantity += operation.quantity
      self.brokerage += operation.brokerage
      self.outcome += operation.net_amount
      self.balance = self.balance - operation.gross_amount
      self.operations << operation
      if self.save
        true
      else
        false
      end
    end
  end


  def sale(operation)
    operation.operation_type_id = 2
    operation.group_id = id
    operation.share_id = share_id
    operation.net_amount = operation.quantity * operation.price
    operation.gross_amount = operation.net_amount - operation.brokerage

    Group.transaction do
      self.quantity -= operation.quantity
      self.brokerage += operation.brokerage
      self.income += operation.net_amount
      self.balance = self.balance + operation.gross_amount
      self.operations << operation
      if self.save
        true
      else
        false
      end
    end
  end


  def split(split, operation)
    operation.operation_type_id = 3
    operation.group_id = id
    operation.share_id = share_id
    operation.comment = split.from.to_s + " : " + split.to.to_s

    Group.transaction do
      self.quantity = (self.quantity / split.from.to_i) * split.to.to_i
      self.operations << operation
      if self.save
        true
      else
        false
      end
    end
  end


  def rightToAcquire(operation)
    operation.operation_type_id = 4
    operation.group_id = id

    subgroup = Subgroup.init
    subgroup.group_id = id
    subgroup.share_id = operation.share_id
    subgroup.quantity = operation.quantity
    subgroup.comment = operation.comment
    subgroup.outcome = 0
    subgroup.brokerage = 0
    subgroup.income = 0
    subgroup.balance = 0


    Group.transaction do
      subgroup.operations << operation
      self.subgroups << subgroup
      if self.save
        true
      else
        false
      end
    end    
    
    
  end

end
